package icbc.app.setting.parser
{
	import icbc.nav.entity.Menu;
	import icbc.nav.entity.MenuChild;

	public class MenuParser extends Parser
	{
		private static const CHILD_NODE:String = "child"; 
		private static const MENU_NAME:String = "name"; 
		private static const MENU_TYPE:String = "type"; 
		private static const MENU_PATH:String = "path"; 
		private static const WIDTH:String = "width"; 
		private static const CHILD_WIDTH:String = "childWidth"; 
		private static const ICON:String = "icon"; 
		private static const CONTENTS:String = "contents"; 
		private static const CONTENT:String = "content"; 

		public function MenuParser(xmlData:XML)
		{
			menu = buildMenu(xmlData);
		}
		
		private var _menu:Menu = null;
		public function get menu():Menu { return _menu; }
		public function set menu(value:Menu):void
		{
			_menu = value;
		}
		
		private function buildMenu(node:XML):Menu
		{
			var item:Menu = new Menu();
			item.childs = buildChilds(node.children());
			item.contents = buildAllContents(node);
			return item;
		}
		
		private function buildChilds(nodes:XMLList):Array
		{
			var list:Array = new Array();
			for each(var node:XML in nodes)
			{
				if(node.name() == CHILD_NODE)
				{
					list.push(buildChild(node));
				}
			}
			return list;
		}

		private function buildContents(nodes:XML):Array
		{
			if(nodes.children().length() == 0) return null;
			var contentsNode:XML = nodes.children()[0];	
			var list:Array = new Array();
			for each(var node:XML in contentsNode.children())
			{
				if(node.name() == CONTENT)
				{
					list.push(buildContent(node));
				}
			}
			return list;
		}
		
		private function buildContent(node:XML):String
		{
			var content:String = node.@[MENU_PATH];
			return content;
		}

		private function buildChild(node:XML):MenuChild
		{
			var item:MenuChild = new MenuChild();
			item.name = node.@[MENU_NAME];
			item.type = node.@[MENU_TYPE];
			item.path = node.@[MENU_PATH];
			item.width = int(node.@[WIDTH]);
			item.childWidth = int(node.@[CHILD_WIDTH]);
			item.icon = node.@[ICON];
			item.contents = buildContents(node);
			item.childs = buildChilds(node.children());
			return item;
		}
		
		private function buildAllContents(node:XML):Array
		{
			var list:Array = [];
			for each(var item:XML in node.elements())
			{
				if(item.name() == CHILD_NODE)
				{
					if(item.@[MENU_TYPE] == "menu")
					{
						buildAllContent(item, list);
					}
				}
			}
			return list;
		}
		
		private function buildAllContent(node:XML, list:Array):void
		{
			for each(var item:XML in node.children())
			{
				if(item.name() == CONTENTS)
				{
					for each(var cont:XML in item.children())
					{
						var mnu:MenuChild = new MenuChild();
						mnu.path = cont.@[MENU_PATH];
						mnu.name = cont.@[MENU_NAME];
						list.push(mnu);
					}
				}
				if(item.name() == CHILD_NODE)
				{
					if(item.@[MENU_TYPE] == CONTENT)
					{
						list.push(buildLiteChild(item));
					}
					if(item.children().length() > 0)
					{
						buildAllContent(item, list);
					}
				}
			}
		}

		private function buildLiteChild(node:XML):MenuChild
		{
			var item:MenuChild = new MenuChild();
			item.name = node.@[MENU_NAME];
			item.type = node.@[MENU_TYPE];
			item.path = node.@[MENU_PATH];
			item.width = int(node.@[WIDTH]);
			item.childWidth = int(node.@[CHILD_WIDTH]);
			item.icon = node.@[ICON];
			item.contents = buildContents(node);
			return item;
		}
	}
}